package com.zzyl.nursing.controller.member;

import com.github.pagehelper.PageHelper;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.dto.ReservationDto;
import com.zzyl.nursing.service.IMemberReservationService;
import com.zzyl.nursing.vo.TimeCountVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 预约信息Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/member/reservation")
@Api(tags =  "预约信息相关接口")
public class MemberReservationController extends BaseController {

    @Autowired
    private IMemberReservationService memberReservationService;
    @GetMapping("/cancelled-count")
    @ApiOperation("查询取消预约数量")
    public R<Integer> getCancelledReservationCount() {
        Long userId = UserThreadLocal.getUserId();
        int count = memberReservationService.getCancelledReservationCount(userId);

        return R.ok(count);
    }
    @GetMapping("/countByTime")
    @ApiOperation("查询每个时间段剩余预约次数")
    public R<List<TimeCountVo>> countReservationsForEachTimeWithinTimeRange(Long time) {
        List<TimeCountVo> list = memberReservationService.countReservationsForTime(time);
        return R.ok(list);
    }
    /**
     * 新增预约信息
     */
    @PostMapping
    @ApiOperation("新增预约信息")
    public AjaxResult addReservations(@RequestBody ReservationDto reservationDto) {

        memberReservationService.addReservations(reservationDto);
        return success();
    }

    /**
     * 分页查询预约信息
     */
    @GetMapping("/page")
    @ApiOperation("分页查询预约信息")
    public R<TableDataInfo<List<Reservation>>> pageQuery(@RequestParam(defaultValue = "1") int pageNum,
                                 @RequestParam(defaultValue = "10") int pageSize,
                                 Integer status) {
        PageHelper.startPage(pageNum,pageSize);
        List<Reservation>  list = memberReservationService.pageQuery(status);
        return R.ok(getDataTable(list));

    }

    @PutMapping("/{id}/cancel")
    @ApiOperation("取消预约")
    public AjaxResult cancelReservation(@PathVariable("id") Long id) {
        memberReservationService.cancelReservation(id);
        return success();
    }
}